<template>
  <div class="login-container">
    <div class="logo-container">
      <div class="logo">
        <img src="@/assets/logo.png" alt="Logo">
      </div>
      <h1 class="app-name">考试系统</h1>
    </div>
    <div class="form-container">
      <van-field v-model="username" placeholder="请输入账号" :border="false" class="input-field" prefix-icon="user">
      </van-field>
      <van-field v-model="password" type="password" placeholder="请输入密码" :border="false" class="input-field"
        prefix-icon="lock">
      </van-field>
      <van-field v-model="name" placeholder="请输入用户名" :border="false" class="input-field" prefix-icon="user">
      </van-field>
      <van-field v-model="email" placeholder="请输入邮箱" :border="false" class="input-field" prefix-icon="user">
      </van-field>

      <van-field v-model="classId" placeholder="请输入班级名称" :border="false" class="input-field" prefix-icon="user">
      </van-field>

      <div class="role-selection">
        <label class="role-label">选择角色</label>
        <van-radio-group v-model="role" class="role-group">
          <van-radio name="student" class="role-option">学生</van-radio>
          <van-radio name="teacher" class="role-option">教师</van-radio>
          <van-radio name="admin" class="role-option">管理员</van-radio>
        </van-radio-group>
      </div>
      <van-button type="primary" block class="login-button" @click="handleRegister">
        注册
      </van-button>
    </div>
  </div>
</template>

<script>
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import { register } from '@/api/userApi.js';

export default {
  name: 'RegisterPage',
  data() {
    return {
      username: '',
      password: '',
      name: '',
      email: '',
      classId: '',
      role: 'student'
    }
  },
  methods: {
    async handleRegister() {
      // 注册表单验证
      if (!this.username) {
        showToast('请输入账号');
        return;
      }
      if (!this.password) {
        showToast('请输入密码');
        return;
      }
      if (!this.name) {
        showToast('请输入用户名');
        return;
      }
      if (!this.email || !/^\S+@\S+\.\S+$/.test(this.email)) {
        showToast('请输入有效的邮箱');
        return;
      }
      if (!this.classId) {
        showToast('请输入有效的班级ID');
        return;
      }

      if (!this.role) {
        showToast('请选择角色');
        return;
      }

      try {
        const res = await register({
          username: this.username,
          password: this.password,
          name: this.name,
          email: this.email,
          role: this.role,
          classId: this.classId
        });

        console.log('注册响应:', res);
        console.log('注册响应数据:', res);

        if (res && res.token) {
          showToast('注册成功');
          this.$router.push('/login');
        } else if (res) {
          showToast('注册成功，请登录');
          this.$router.push('/login');
        } else {
          showToast(res.message || res.msg || '注册失败，请重试');
        }
      } catch (error) {
        console.error('注册失败', error);
        showToast('网络错误，请重试');
      }
    }
  },
  created() {
    // 初始化路由
    this.$router = useRouter();
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 0 20px;
}

.logo-container {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  width: 100px;
  height: 100px;
  background: white;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 auto 20px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}

.logo img {
  width: 60px;
  height: 60px;
}

.app-name {
  color: white;
  font-size: 24px;
  font-weight: bold;
}

.form-container {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 12px;
  padding: 30px 20px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.input-field {
  margin-bottom: 20px;
  background: #f5f5f5;
  border-radius: 8px;
  padding: 0 15px;
}

.login-button {
  margin-top: 30px;
  background: #667eea;
  border-radius: 8px;
  height: 50px;
  line-height: 50px;
  font-size: 16px;
}

.role-selection {
  margin-bottom: 20px;
}

.role-label {
  display: block;
  font-size: 14px;
  color: #666;
  margin-bottom: 10px;
}

.role-group {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

.role-option {
  display: inline-flex;
  align-items: center;
}

.register-link {
  text-align: center;
  margin-top: 20px;
  color: #666;
}

.register-link a {
  color: #667eea;
  text-decoration: none;
}
</style>